package com.jnu.student.myfirstapplication.ui.task;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.ViewModelProvider;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;

import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
import com.jnu.student.myfirstapplication.databinding.FragmentTaskBinding;

public class TaskFragment extends Fragment {

    private TaskViewModel mViewModel;
    private final String []tabHeaderStrings = {"每日任务","每周任务","普通任务"};
    private FragmentTaskBinding binding;

//    public static TaskFragment newInstance() {
//        return new TaskFragment();
//    }
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        TaskViewModel taskViewModel =
                new ViewModelProvider(this).get(TaskViewModel.class);
        binding = FragmentTaskBinding.inflate(inflater, container, false);
        View root = binding.getRoot();
//        return inflater.inflate(R.layout.fragment_task, container, false);
        // 获取ViewPager2和TabLayout的实例
        TabLayout tabLayout = binding.tabLayoutTasks;
        ViewPager2 viewPager = binding.viewPager2Tasks;
        // 创建适配器
        FragmentAdapter fragmentAdapter = new FragmentAdapter(getChildFragmentManager(), getLifecycle());
        viewPager.setAdapter(fragmentAdapter);

        // 将TabLayout和ViewPager2进行关联
        new TabLayoutMediator(tabLayout, viewPager,
                (tab, position) -> tab.setText(tabHeaderStrings[position])).attach();
        return root;
    }

    public class FragmentAdapter extends FragmentStateAdapter {
        private static final int NUM_TABS = 3;
        public FragmentAdapter(@NonNull FragmentManager fragmentManager, @NonNull Lifecycle lifecycle) {
            super(fragmentManager, lifecycle);
        }

        @NonNull
        @Override
        public Fragment createFragment(int position) {
            // 根据位置返回对应的Fragment实例
            switch (position) {
                case 0:
                    return new OneTaskFragment().newInstance("DaliyTask.Data", "FinishedDaliyTask.Data");
//                    return new OneTaskFragment();        // new BookListFragment()
                case 1:
                    return new OneTaskFragment().newInstance("WeeklyTask.Data", "FinishedWeeklyTask.Data");
//                    return new OneTaskFragment();         // new TencentMapFragment()
                case 2:
                    return new OneTaskFragment().newInstance("SimpleTask.Data", "FinishedSimpleTask.Data");
//                    return new OneTaskFragment();        // new WebViewFragment()
                default:
                    return null;
            }
        }

        @Override
        public int getItemCount() {
            return NUM_TABS;
        }
    }
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mViewModel = new ViewModelProvider(this).get(TaskViewModel.class);
    }
}